package com.jason.www.base;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.gyf.immersionbar.ImmersionBar;
import com.jason.www.R;
import com.jason.www.config.Constants;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import org.greenrobot.eventbus.EventBus;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.app.SkinAppCompatDelegateImpl;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import skin.support.content.res.SkinCompatResources;
import skin.support.widget.SkinCompatSupportable;

/**
 * @author：Jason
 * @date：2020/8/4 10:36
 * @email：1129847330@qq.com
 * @description:
 */
public abstract class BaseActivity extends AppCompatActivity implements SkinCompatSupportable {
    private Unbinder unbinder;
    protected Context mContext;
    protected AppCompatActivity mActivity;
    private Toast toast;
    protected final int PAGE_START = Constants.PAGE_START;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        mContext = this;
        mActivity = this;
        super.onCreate(savedInstanceState);
        beforeSetContentView();
        setContentView(getLayoutResId());
        ActivityManagerDelegate.getInstance().addActivity(this);
        unbinder = ButterKnife.bind(this);
        initMvp();
        initView();
        initData();
        initEvent();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initImmersionBar();
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        initImmersionBar();
    }

    protected void initImmersionBar() {
        //设置共同沉浸式样式
        ImmersionBar.with(this)
                .keyboardEnable(true)
                .init();
    }

    protected void initMvp() {
    }

    protected void initView() {
    }

    protected void initData() {
    }

    protected void initEvent() {
    }

    protected void beforeSetContentView() {
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (isRegisterEventBus()) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (isRegisterEventBus()) {
            EventBus.getDefault().unregister(this);
        }
    }

    protected boolean isRegisterEventBus() {
        return false;
    }

    protected void showToast(String msg) {
        if (toast != null) {
            toast.cancel();
        }
        toast = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
        toast.show();
    }

    protected abstract int getLayoutResId();

    protected void gotoPage(Class targetActivity) {
        startActivity(new Intent(mContext, targetActivity));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManagerDelegate.getInstance().finishActivity(this);
        unbinder.unbind();
        unbinder = null;
    }

    protected void startActivity(Class<?> activity) {
        startActivity(new Intent(mContext, activity));
    }

    @NonNull
    @Override
    public AppCompatDelegate getDelegate() {
        return SkinAppCompatDelegateImpl.get(this, this);
    }

    @Override
    public void applySkin() {
        Log.d("skin", "applySkin: 换肤成功，更改状态栏颜色");
        updateStatusBarColor();
    }

    private void updateStatusBarColor() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(SkinCompatResources.getColor(this, R.color.colorPrimary));
        }
        // 修改状态栏字体颜色
        boolean useDarkStatusBar = false;
        //boolean useDarkStatusBar = getResources().getBoolean(R.bool.use_dark_status);
        //int resId = SkinCompatResources.getInstance().getTargetResId(this, R.bool.use_dark_status);
        //if (resId != 0) {
        //    useDarkStatusBar = SkinCompatResources.getInstance().getSkinResources().getBoolean(resId);
        //}
        if (useDarkStatusBar) {
            setDarkModeStatusBar();
        } else {
            setLightModeStatusBar();
        }
    }

    /**
     * 状态栏文本颜色为白色，且背景透明
     */
    protected void setLightModeStatusBar() {
        ImmersionBar.with(this).statusBarDarkFont(false).statusBarColor(R.color.transparent).init();
    }

    /**
     * 状态栏文本颜色为黑色，且背景透明
     */
    protected void setDarkModeStatusBar() {
        ImmersionBar.with(this).statusBarDarkFont(true).statusBarColor(R.color.transparent).init();
    }

    private LoadingDialog loadingDialog;

    public void showLoading(String msg) {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(mContext);
            loadingDialog.setLoadingText(msg);
            loadingDialog.setInterceptBack(false);
        }
        loadingDialog.show();
    }

    public void showLoading() {
        showLoading("加载中");
    }

    public void hideLoading() {
        if (loadingDialog != null) {
            loadingDialog.close();
        }
    }
}